﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using DAO;
using Entidades;

namespace ProyectoCine
{
    public partial class MostrarPelicula : System.Web.UI.Page, IGrilla
    {
        protected void Page_Load(object sender, EventArgs e)
        {
            ViewState["Orden"] = "";
            if (Session["id_Pelicula"] != null)
            {
                Pelicula p = PeliculaDAO.buscarPorId((int)Session["id_Pelicula"]);
                panelPelicula.GroupingText = p.Nombre;
                lblNombre.Text = p.Nombre;
                lblSinopsis.Text= p.Sipnosis;
                lblPaisOrigen.Text = p.PaisOrigen;
                lblInterpretes.Text = p.Interpretes;
                lblDirector.Text = p.Director;
                lblClasificacion.Text = p.Clasificacion;
                lblDuracion.Text =p.Duracion.ToString("HH:mm");
                lblAñoEstreno.Text = p.AñoEstreno.Year.ToString();
                lblGenero.Text = PeliculaDAO.buscarNombreGenero(p.Genero);
                cargarGrilla();              
                
            }
        }

        public void cargarGrilla()
        {
            gvHorarios.DataKeyNames = new string[] { "id_Pelicula","id_Cine","numero_Sala","id_Horario","fecha" };
            gvHorarios.DataSource = FuncionDAO.cargarGrillaPelicula((int)Session["id_Pelicula"], ViewState["Orden"].ToString());
            gvHorarios.DataBind();
        }

        protected void gvHorarios_SelectedIndexChanged(object sender, EventArgs e)
        {
            Entidades.Funcion f = new Entidades.Funcion();
            f.IdPelicula = (int)Session["id_Pelicula"];
            f.IdCine = (int)gvHorarios.DataKeys[gvHorarios.SelectedIndex].Values["id_Cine"];
            f.NumeroSala = (int)gvHorarios.DataKeys[gvHorarios.SelectedIndex].Values["numero_Sala"];
            f.IdHorario = (int)gvHorarios.DataKeys[gvHorarios.SelectedIndex].Values["id_Horario"];
            f.Fecha=(DateTime)gvHorarios.DataKeys[gvHorarios.SelectedIndex].Values["fecha"];
            Session["id_Funcion"] = f;
            Response.Redirect("Usuarios/Reservar.aspx");
        }

        protected void gvHorarios_PageIndexChanging(object sender, GridViewPageEventArgs e)
        {
            gvHorarios.PageIndex = e.NewPageIndex;
            cargarGrilla();
        }

        protected void gvHorarios_Sorting(object sender, GridViewSortEventArgs e)
        {
            ViewState["Orden"] = e.SortExpression;
            cargarGrilla();
        }

        protected void btnVolver_Click(object sender, EventArgs e)
        {
            Response.Redirect("Cartelera.aspx");
        }
    }
}